package com.example.deertrip;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.deertrip.bean.Moment;
import com.example.deertrip.database.DBOperate;

public class PostCommunityActivity extends AppCompatActivity {
   private Button btn_post;
   private TextView username_tv;
   private ImageView postimage_iv;
   private EditText input;
    DBOperate dbOperate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_community);
        btn_post=findViewById(R.id.society_push);
        username_tv=findViewById(R.id.society_username);
        postimage_iv=findViewById(R.id.society_iv);
        input=findViewById(R.id.society_input);


        dbOperate=new DBOperate(this);
        lister();

    }

    private void lister() {
        btn_post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        SharedPreferences sp=this.getSharedPreferences("deertrip",MODE_PRIVATE);
        String username=sp.getString("UserName","您");
        username_tv.setText("欢迎 "+username+" 用户发布动态！");

        postimage_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                        startActivityForResult(selectPicture(),1);
            }
        });

        btn_post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username1=username;
                String content=input.getText().toString();
                Integer good=0;
                postimage_iv.setDrawingCacheEnabled(true);
                Bitmap bitmap=postimage_iv.getDrawingCache();
                Moment moment=new Moment(content,username,good,bitmap);
                dbOperate.saveMoment(moment);
                postimage_iv.setDrawingCacheEnabled(false);
                Toast.makeText(PostCommunityActivity.this, "发布动态成功", Toast.LENGTH_LONG).show();
            }
        });

    }

    public Intent selectPicture(){
        Intent intent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        return intent;
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data) {

        if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            Bitmap bp = BitmapFactory.decodeFile(picturePath);
            postimage_iv.setImageBitmap(bp);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}